package ase.assignment.sts.ws.impl;

import java.rmi.RemoteException;

import ase.assignment.sts.api.CustomerHandler;
import ase.assignment.sts.beans.CustomerEntity;
import ase.assignment.sts.ws.beans.WSUtil;
import ase.assignment.sts.ws.customer.CustomerPortType;
import ase.assignment.sts.ws.customer.CustomerServiceLocator;

public class CustomerHandlerWSImpl implements CustomerHandler {
	private CustomerPortType portType;

	CustomerHandlerWSImpl() {
		try {
			portType = new CustomerServiceLocator().getSTSCustomer();
		} catch (javax.xml.rpc.ServiceException jre) {
			if (jre.getLinkedCause() != null)
				jre.getLinkedCause().printStackTrace();
		}
	}

	public void close(String username) {
		try {
			portType.close(username);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	public void create(CustomerEntity entity) {
		try {
			portType.create(WSUtil.convert(entity));
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

	public CustomerEntity find(String username) {
		try {
			return WSUtil.convert(portType.find(username));
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean login(String username, String password) {
		try {
			return portType.login(username, password);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return false;
	}

	public void update(CustomerEntity entity) {
		try {
			portType.update(WSUtil.convert(entity));
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}

}
